package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.commen.BaseContext;
import com.itheima.reggie.commen.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "LoginCheckFilter", value = "/*")
public class LoginCheckFilter implements Filter {
    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String url = req.getRequestURL().toString();
        log.info("拦截到请求：{}",url);
        String[] urls = {
                "/backend/",
                "/front/",
                "/employee/logout",
                "/employee/login",
                "/user/sendMsg",
                "/user/login",
                "/doc.html",
                "/webjars/",
                "/swagger-resources",
                "/v2/api-docs"
        };
        for (String u : urls) {
            if (url.contains(u)) {
                log.info("本次请求{}不需要处理",u);
                chain.doFilter(req,resp);
                return;
            }
        }
//        if (check(url,urls)) {
//            log.info("本次请求{}不需要处理",url);
//                chain.doFilter(req,resp);
//                return;
//        }
        HttpSession session = req.getSession();
        if (session.getAttribute("employee") != null) {
            log.info("用户已登录，id：{}",session.getAttribute("employee"));
//            Long empID = (Long) session.getAttribute("employee");
//            BaseContext.setCurrentId(empID);
            chain.doFilter(req,resp);
            return;
        }

        if (session.getAttribute("user") != null) {
            log.info("用户已登录，id：{}",session.getAttribute("user"));
            Long userID = (Long) session.getAttribute("user");
            BaseContext.setCurrentId(userID);
            chain.doFilter(req,resp);
            return;
        }
        log.info("用户未登录");
        resp.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
    }

    /**
     * 路径匹配，检查本次请求是否需要放行
     * @param url
     * @param urls
     * @return
     */
    private boolean check(String url,String[] urls){
        for (String u : urls) {
            boolean match = PATH_MATCHER.match(u, url);
            if (match) {
                return true;
            }
        }
        return false;
    }
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }


}
